package com.qianli.cashmama.trade.repo.impl;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.qianli.cashmama.trade.dao.TBillAccountingDao;
import com.qianli.cashmama.trade.domain.model.BillAccounting;
import com.qianli.cashmama.trade.entity.TBillAccountingEntity;
import com.qianli.cashmama.trade.repo.BillAccountingRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 * User: pavan
 * Date: 2019/11/20
 * Time: 下午11:57
 */
@Repository
public class BillAccountingRepositoryImpl implements BillAccountingRepository {

    @Autowired
    private TBillAccountingDao tBillAccountingDao;

    /**
     * @param billNo
     * @return
     */
    @Override
    public List<BillAccounting> getBillAccountingByBillNo(String billNo) {
        List<TBillAccountingEntity> entityList = tBillAccountingDao.selectByBillNo(billNo);
        if (CollectionUtils.isEmpty(entityList)) {
            return new ArrayList<>();
        }
        List<BillAccounting> billAccountingList = entityList.stream().map(item -> {
            BillAccounting billAccounting = new BillAccounting();
            BeanUtils.copyProperties(item, billAccounting);
            return billAccounting;
        }).collect(Collectors.toList());
        return billAccountingList;
    }

    /**
     * @param payBizNo
     * @return
     */
    @Override
    public BillAccounting getBillAccountingByPayBizNo(String payBizNo) {
        if (StringUtils.isEmpty(payBizNo)) {
            return null;
        }
        TBillAccountingEntity entity = tBillAccountingDao.selectByPayBillNo(payBizNo);
        if (entity == null) {
            return null;
        }
        BillAccounting billAccounting = new BillAccounting();
        BeanUtils.copyProperties(entity, billAccounting);
        return billAccounting;
    }

    /**
     * @param billAccounting
     */
    @Override
    public void store(BillAccounting billAccounting) {
        TBillAccountingEntity entity = new TBillAccountingEntity();
        BeanUtils.copyProperties(billAccounting, entity);
        tBillAccountingDao.insert(entity);
    }
}
